var express = require('express');
var router = express.Router();
var sqlite3 = require('sqlite3').verbose();
var path = require('path');
/* GET users listing. */
router.get('/', function(req, res, next) {
    var db=new sqlite3.Database(path.join(__dirname, "../db/Delphi.s3db"));
    if(req.query.status==2){//显示职业专家
        try{
            sqlStr1 = "SELECT * FROM p_expert where type='2'";
            var enquiry = req.query.searchParams;
                console.log(enquiry);

                if(enquiry!=1)
                {
                    add1 = (enquiry.p_expert_name=="")?"":" and p_expert_name like '"+enquiry.p_expert_name+"%'";
                    add2 = (enquiry.p_expert_id=="")?"":" and p_expert_id like '"+enquiry.p_expert_id+"%'";
                    sqlStr1=sqlStr1+add1+add2;
                }
            db.all(sqlStr1,function(err,rows){
                if(!rows||rows.length==0){
                    res.send('0');
                }else{
                    table={}
                    table.data=rows;
                    table.count=rows.length;
                    res.status(200).json(table);
                }
            } );
        }catch(ex){
            console.log(ex);
        }
    }
    else if(req.query.status==1){//显示学生专家
        try{
            sqlStr2 = "SELECT * FROM s_expert where type='1'";
            var enquiry = req.query.searchParams;
                console.log(enquiry);
                if(enquiry!=1)
                {
                    add1 = (enquiry.s_expert_name=="")?"":" and s_expert_name like '"+enquiry.s_expert_name+"%'";
                    add2 = (enquiry.s_expert_id=="")?"":" and s_expert_id like '"+enquiry.s_expert_id+"%'";
                    sqlStr2=sqlStr2+add1+add2;
                }
            db.all(sqlStr2,function(err,rows){
                if(!rows||rows.length==0){
                    res.send('0');
                }else{
                    table={}
                    table.data=rows;
                    table.count=rows.length;
                    res.status(200).json(table);
                }
            } );
        }catch(ex){
            console.log(ex);
        }
    }  else if(req.query.status==3){//显示专家个人视角图
        try{
            sqlStr1 = "SELECT * FROM p_expert where p_expert_id=? ";
            sqlStr2 = "SELECT * FROM s_expert where s_expert_id=? ";
            db.all(sqlStr1,req.session.username,function(err,rows){
            if(!rows||rows.length==0){
                db.all(sqlStr2,req.session.username,function(err,rows){
                    if(!rows||rows.length==0){
                            res.send('0');
                    }else{
                            table={}
                            table.data=rows;
                            table.count=rows.length;
                            res.status(200).json(table);
                         }
                } );
            }else{
                    table={}
                    table.data=rows;
                    table.count=rows.length;
                    res.status(200).json(table);
                 }
        } );
        }catch(ex){
            console.log(ex);
        }
    }

});

module.exports = router;
